//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/image/canyimage.h>

namespace buola { namespace img {

template<typename tTarget>
static inline CImage<tTarget> convert_to(const CAnyImage &pImage)
{
    CImage<tTarget> lDest(pImage.Size());

    switch(pImage.Format())
    {
    case FORMAT_RGB8:
        convert_pixels(pImage.View<img::TFormat_rgb8>(),lDest);
        break;
    case FORMAT_RGBA8:
        convert_pixels(pImage.View<img::TFormat_rgba8>(),lDest);
        break;
    case FORMAT_GRAY8:
        convert_pixels(pImage.View<img::TFormat_gray8>(),lDest);
        break;
    case FORMAT_GRAY32:
        convert_pixels(pImage.View<img::TFormat_gray32>(),lDest);
        break;
    default:
        throw XInvalid("invalid conversion in CAnyImage::Convert");
    }

    return lDest;
}
    
void CAnyImage::Convert(EImageFormat pFormat)
{
    if(pFormat==Format()) return;
    
    switch(pFormat)
    {
    case FORMAT_RGB8:
        *this=convert_to<img::TFormat_rgb8>(*this);
        return;
    case FORMAT_RGBA8:
        *this=convert_to<img::TFormat_rgba8>(*this);
        return;
    case FORMAT_GRAY8:
        *this=convert_to<img::TFormat_gray8>(*this);
        return;
    case FORMAT_GRAY32:
        *this=convert_to<img::TFormat_gray32>(*this);
        return;
    default:
        throw XInvalid("invalid conversion in CAnyImage::Convert");
    }
}
    
/*namespace img*/ } /*namespace buola*/ }
